// pages/admin-goods/admin-goods.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenmodalput: true,
    hiddenmodalputadd: true,
    hiddenName: true,
    default: '点击选择',

  },
  bindPickerChangeedit: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    let index = e.detail.value
    let array = this.data.arrxx
    this.setData({
      index: e.detail.value,
      gtype: array[index].name
    })
  },

  modal_edit: function (e) {
    var id = e.currentTarget.dataset.id
    var gimg = e.currentTarget.dataset.gimg
    var gname = e.currentTarget.dataset.gname
    var gnews = e.currentTarget.dataset.gnews
    var gprice = e.currentTarget.dataset.gprice
    var gnumber = e.currentTarget.dataset.gnumber
    var gshop = e.currentTarget.dataset.gshop
    var garea = e.currentTarget.dataset.garea
    var gtype = e.currentTarget.dataset.gtype

    // console.log(id, gimg, gname, gnews, gprice, gnumber, gshop, garea)
    this.setData({
      id: id,
      gimg: gimg,
      gname: gname,
      gnews: gnews,
      gprice: gprice,
      gnumber: gnumber,
      gshop: gshop,
      garea: garea,
      gtype: gtype,
      hiddenmodalput: !this.data.hiddenmodalput
    })

  },
  cancelM: function (e) {
    this.setData({
      hiddenmodalput: true,
    })
  },

  confirmM: function (e) {
    console.log("商品图片：" + this.data.gimg + "商品名：" + this.data.gname + "  详情：" + this.data.gnews + "  价格：" + this.data.gprice + "  库存：" + this.data.gnumber + "  店铺名：" + this.data.gshop + "  商品产地：" + this.data.garea);
    // console.log(this.data)
    // console.log(this.data.id)
    let key = this.data.arrxx[this.data.index].name;
    let that = this;
    wx.request({
      url: 'http://localhost/shopping/admin_goods_edit.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        id: that.data.id,
        goodsimg: that.data.gimg,
        goodsname: that.data.gname,
        goodsnews: that.data.gnews,
        price: that.data.gprice,
        number: that.data.gnumber,
        shop: that.data.gshop,
        area: that.data.garea,
        type: key,
      },
      success: function (res) {
        console.log(res.data);
        console.log('修改商品成功！')
        that.Goods();
        that.setData({})
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    this.setData({
      hiddenmodalput: true
    })
    that.onShow();

  },

  iName: function (e) {
    this.setData({
      gname: e.detail.value,
    })
  },
  iNews: function (e) {
    this.setData({
      gnews: e.detail.value
    })
  },
  iPrice: function (e) {
    this.setData({
      gprice: e.detail.value
    })
  },
  iNumber: function (e) {
    this.setData({
      gnumber: e.detail.value
    })
  },
  iShop: function (e) {
    this.setData({
      gshop: e.detail.value
    })
  },
  iArea: function (e) {
    this.setData({
      garea: e.detail.value
    })
  },
  chooseImage() {
    wx.chooseImage({
      success: res => {
        console.log(res)
        this.setData({
          gimg: res.tempFilePaths[0]
        })
      }
    })
  },
  modal_add: function (e) {
    var aid = e.currentTarget.dataset.aid
    var agoodsimg = e.currentTarget.dataset.agimg
    var agoodsname = e.currentTarget.dataset.agname
    var agoodsnews = e.currentTarget.dataset.agnews
    var aprice = e.currentTarget.dataset.agprice
    var anumber = e.currentTarget.dataset.anumber
    var ashop = e.currentTarget.dataset.ashop
    var aarea = e.currentTarget.dataset.aarea
    // console.log(aid, agoodsimg, agoodsname, agoodsnews, aprice, anumber, ashop, aarea)
    this.setData({
      aid: aid,
      agoodsimg: agoodsimg,
      agoodsname: agoodsname,
      agoodsnews: agoodsnews,
      aprice: aprice,
      anumber: anumber,
      ashop: ashop,
      aarea: aarea,
      hiddenmodalputadd: !this.data.hiddenmodalputadd
    })

  },
  cancelMadd: function (e) {
    this.setData({
      hiddenmodalputadd: true,
    })
  },

  confirmMadd: function (e) {
    if (this.data.imgadd == null || this.data.nameadd == null || this.data.newsadd == null || this.data.priceadd == null || this.data.numberadd == null || this.data.shopadd == null || this.data.areaadd == null) {
      wx.showModal({
        title: '',
        content: '内容不可为空！',
      })
    } else {
      console.log("商品图片：" + this.data.imgadd + "商品名：" + this.data.nameadd + "  详情：" + this.data.newsadd + "  详情：" + this.data.priceadd + "  库存：" + this.data.numberadd + "  店铺名：" + this.data.shopadd + "  产地：" + this.data.areaadd);
      let key = this.data.arrxx[this.data.index].name;
      console.log(key)
      var that = this;
      wx.request({
        url: 'http://localhost/shopping/admin_goods_add.php',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          addid: that.data.idadd,
          addgoodsimg: that.data.imgadd,
          addgoodsname: that.data.nameadd,
          addgoodsnews: that.data.newsadd,
          addprice: that.data.priceadd,
          addnumber: that.data.numberadd,
          addshop: that.data.shopadd,
          addarea: that.data.areaadd,
          type: key,
        },
        success: function (res) {
          // console.log(res.data);
          console.log('添加商品成功！')
          that.Goods();
          that.setData({})
        },
        fail: function (res) {
          console.log("fail");
        }
      })
      this.setData({
        hiddenmodalputadd: true
      })
    }
  },
  iNameadd: function (e) {
    this.setData({
      nameadd: e.detail.value,
    })
  },
  iNewsadd: function (e) {
    this.setData({
      newsadd: e.detail.value
    })
  },
  iPriceadd: function (e) {
    this.setData({
      priceadd: e.detail.value
    })
  },
  iNumberadd: function (e) {
    this.setData({
      numberadd: e.detail.value
    })
  },
  iShopadd: function (e) {
    this.setData({
      shopadd: e.detail.value
    })
  },
  iAreaadd: function (e) {
    this.setData({
      areaadd: e.detail.value
    })
  },
  addchooseImage() {
    wx.chooseImage({
      success: res => {
        console.log(res)
        this.setData({
          imgadd: res.tempFilePaths[0]
        })
      }
    })
  },
  modal_del: function (e) {
    var id = e.currentTarget.dataset.delid
    console.log(id)
    let that = this;
    wx.showModal({
      title: '',
      content: '是否确认删除',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: 'http://localhost/shopping/admin_goods_del.php',
            header: {
              'Content-Type': 'application/json'
            },
            data: {
              id: id,
            },
            success: function (res) {
              // console.log(res.data);
              console.log('删除商品成功！')
              that.Goods();
              that.setData({})
            },
            fail: function (res) {
              console.log("fail");
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })


    this.setData({})
  },
  modal_info: function (e) {
    var id = e.currentTarget.dataset.id

    wx.navigateTo({
      url: '/pages/admin-goods-info/admin-goods-info?id=' + id + '',
    })
  },
  bindKeyInput: function (e) {
    this.setData({
      key: e.detail.value
    })
  },
  search: function (e) {
    // console.log(e)
    let that = this;
    // console.log(that.data.key);
    wx.request({
      url: 'http://localhost/shopping/admin_goods_search.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        key: that.data.key,
      },
      success: function (res) {
        // console.log(res.data);
        that.setData({
          arraa: res.data.goodssearch,
          hiddenName: !that.data.hiddenName
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })

  },
  bindPickerChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    let index = e.detail.value
    let array = this.data.arrxx
    this.setData({
      index: e.detail.value,
      default: array[index].name
    })
  },
  Goods: function () {
    var that = this;
    wx.request({
      url: 'http://localhost/shopping/admin_goods.php',
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        // console.log(res.data)
        that.setData({
          arr: res.data.goods,

        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
    wx.request({
      url: 'http://localhost/shopping/admin_type.php',
      header: {
        'Content-Type': 'application/json'
      },
      data: {},
      success: function (res) {
        console.log(res.data);
        that.setData({
          arrxx: res.data.type,
        })
      },
      fail: function (res) {
        console.log("fail");
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.Goods();

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})